package com.ibeetl.restful;


import com.alibaba.fastjson.JSONObject;
import com.ibeetl.cms.entity.XxdOrder;
import com.ibeetl.cms.service.XxdOrderService;
import com.ibeetl.cms.util.AESUtils;
import com.ibeetl.cms.util.CheckSecurityUtil;
import com.ibeetl.vo.BaseDataVo;
import com.ibeetl.vo.RespBaseVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商户信息表 控制器
 * </p>
 */
@Slf4j
@Api(tags = "订单信息表接口")
@RestController
@RequestMapping("/rest")
public class OrderRestful {

    @Autowired
    private XxdOrderService xxdOrderService;

    @GetMapping("getById")
    @ApiOperation("根据id查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "String", paramType = "body"),
    })
    public RespBaseVo getById(BaseDataVo vo, String id) {
        RespBaseVo checkMap= CheckSecurityUtil.preCheck(vo);
        if(!checkMap.getSuccess()){
            return checkMap;
        }
        if(id==null){
            return RespBaseVo.fail("订单id不能为空");
        }
        List<XxdOrder> list = xxdOrderService.getById(id);
        if(list != null && list.size() != 0){
            return  RespBaseVo.success(list.get(0));
        }
        return RespBaseVo.success("未查到此订单");
    }

//    @PostMapping("addMerchantForMask")
//    @ApiOperation("保存商户数据（口罩领取活动接口）")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "merchantCode", value = "商户编号", required = true, dataType = "String", paramType = "body"),
//            @ApiImplicitParam(name = "receiveName", value = "收货人姓名", required = true, dataType = "String", paramType = "body"),
//            @ApiImplicitParam(name = "receivePhone", value = "收货人电话", required = true, dataType = "String", paramType = "body"),
//            @ApiImplicitParam(name = "receiveAddress", value = "收货人地址", required = true, dataType = "String", paramType = "body")
//    })
//    public RespBaseVo addMerchantForMask(String merchantCode,String receiveName,String receivePhone,String receiveAddress){
//
//        if(StringUtils.isEmpty(merchantCode)){
//            return RespBaseVo.success("商户编码为空");
//        }
//        if(StringUtils.isEmpty(receiveName)){
//            return RespBaseVo.success("收货人姓名为空");
//        }
//        if(StringUtils.isEmpty(receivePhone)){
//            return RespBaseVo.success("收货人手机号为空");
//        }
//        if(StringUtils.isEmpty(receiveAddress)){
//            return RespBaseVo.success("收货人地址为空");
//        }
//
//        MerchantEventRecord record=merchantEventRecordMapper.selectOne(
//                new MerchantEventRecord()
//                        .setMerchantCode(merchantCode)
//                        .setEventType(0)
//                        .setDel("0")
//        );
//        if(record == null ){
//            return RespBaseVo.success("对不起，您不是活动商户，无法参与本次活动。");
//        }
//        if(record.getStatus()==1){
//            return RespBaseVo.success("您已参与过本次活动，请勿重复提交");
//        }
//        record.setReceiverName(receiveName);
//        record.setReveiverPhone(receivePhone);
//        record.setReceiverAddress(receiveAddress);
//        record.setUpdatedTime(new Date());
//        record.setStatus(1);//已参加
//        merchantEventRecordMapper.updateById(record);
//        return RespBaseVo.success("信息登记成功");
//    }

    @GetMapping(value = "/signGenerate")
    @ApiOperation("signKey生成（供测试使用）")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "merchantCode", value = "商户编号", required = true, dataType = "String", paramType = "body")
//    })
    public void signGenerate(HttpServletResponse response) throws IOException {
        JSONObject obj=new JSONObject();
        //obj.put("merchantCode",merchantCode); //商户编号
        obj.put("time",System.currentTimeMillis()/1000);//时间 单位秒

        String aesKey=  "kieodlgnxxdqwerp";//Aes密钥key
        String s = AESUtils.encryptAES(obj.toString(), aesKey);
        String signKey=java.net.URLEncoder.encode(AESUtils.encryptAES(obj.toString(),aesKey),"UTF-8");
        System.out.println("加密密文："+s);

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(signKey);
        out.flush();
        out.close();
    }


}

